<?php

namespace mz\ars;

use mz\service\FormatCategory;
use Yii;

/**
 * This is the model class for table "{{%category}}".
 *
 * @property int $id 分类id
 * @property int $pid 父类id
 * @property string $name 分类名称
 * @property string $desc 分类描述
 */
class Category extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%category}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['pid'], 'integer'],
            [['name', 'desc'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'pid' => 'Pid',
            'name' => 'Name',
            'desc' => 'Desc',
        ];
    }

    # 返回所有分类
    static public function getAllCategory()
    {
        $cate = self::find()->asArray()->all();
        return FormatCategory::unlimitedForLevel($cate);
    }

    # 获取父级分类
    static public function getAllFather($where)
    {
        return self::find()->where($where)->asArray()->all();
    }

    # 获取分类详情
    static function findModel($id)
    {
        $cateDetail = self::find()->where(['id' => $id])->asArray()->one();
        return $cateDetail;
    }

    # 获取单个父类详情
    static public function getFatherDetail($pid)
    {
        if (empty($pid))return ['pid' => 0,'pname' => '根目录'];
        $pInfo = self::findModel($pid);

        return ['pname' =>$pInfo['name'],'pid' => $pInfo['id']];
    }
}
